/**
 *   Copyright (c) fanguangping. All rights reserved.
 *   The use and distribution terms for this software are covered by the
 *   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
 *   which can be found in the file epl-v10.html at the root of this distribution.
 *   By using this software in any fashion, you are agreeing to be bound by
 * 	 the terms of this license.
 *   You must not remove this notice, or any other, from this software.
 **/
package org.jfunny.core.mapping;

import java.util.LinkedList;
import java.util.List;

import org.jfunny.core.dfa.DFA;
import org.jfunny.core.dfa.State;
import org.jfunny.core.ds.Statement;
import org.jfunny.core.util.StringUtil;

/**
 * 抽象模板映射
 * 
 * @author fanguangping
 *
 */
public abstract class AbstractMapping implements IMapping {
	private String key;
	private String value;
	private String keyName;

	private List<Statement> keyTemplate;
	private List<Statement> valueTemplate;
	private State starter;

	public AbstractMapping(String keyName, String key, String value) {
		this.key = key;
		this.value = value;
		this.keyName = keyName;

		keyTemplate = new LinkedList<Statement>();
		keyTemplate.addAll(getTemplate(key));

		valueTemplate = new LinkedList<Statement>();
		valueTemplate.addAll(getTemplate(value));

		starter = DFA.getInstance().toDFA(keyTemplate);
	}

	protected abstract List<Statement> getTemplate(String statement);

	@Override
	public String getKey() {
		return key;
	}

	@Override
	public String getValue() {
		return value;
	}

	@Override
	public List<Statement> getKeyTemplate() {
		return keyTemplate;
	}

	@Override
	public List<Statement> getValueTemplate() {
		return valueTemplate;
	}

	@Override
	public String getKeyName() {
		return keyName;
	}

	@Override
	public String getValueName() {
		String funcName = StringUtil.substringAfter(value, "(");
		funcName = funcName.trim();
		funcName = StringUtil.substringBefore(funcName, " ");
		return funcName;
	}

	@Override
	public State getStarter() {
		return starter;
	}
}
